package core;

import dbManagement.dbManager;
import dbManagement.notMyPatientException;
import hirondelle.date4j.DateTime;
import java.io.Serializable;
import logManagement.Log4k;
import mailManagement.Mailer;
import objects.Macro;
import objects.userKind;
import objects.visitKind;
import pdfManagement.PdfException;
import pdfManagement.pdfPrinter;

/**
 *
 * @author administrator
 */
public class VisitBean implements Serializable{
    private final static String linkUrgentImg = Macro.URGIMGPATH+"urgent.jpg";
    private final static String linkExpiredImg = Macro.URGIMGPATH+"expired.jpg"; 
    private final static String linkFutureImg = Macro.URGIMGPATH+"future.png"; 
    private final static int urgentRange = 3600; //secondi per calcolare l'urgenza della visita
    
    private final static String linkNegligentImg = Macro.URGIMGPATH+"negligent.png";
    private final static String linkPunctualImg = Macro.URGIMGPATH+"punctual.png";
    
    private int interval;
    
    private int index;
    private int visit_id; 
    private int patient_id;
    private int doctor_id;
    private int program_id = -1; /* default value if not set */
    private DateTime date;
    private visitKind type;
    private String report;
    private int reminder;
    private boolean visited;       
    private String patient_name;    
    private String doctor_name;
    
    private boolean voidReport = false;
    
    private String error; /* for Log4k.debug function */
    
    /*
     * Aggiunge una visita e ritorna il suo id.
     * Da utilizzare solo dopo aver settato tutti i campi
     */
    public int addVisit(){
        int ret = -1;
        error = "Chiamata addVisit";
        Log4k.debug(this.getClass().getName(), error);
                
        try {
            dbManager db = new dbManager();
            ret = db.addVisit(patient_id, doctor_id, program_id, date, type, userKind.DOCTOR);
            db.releaseConnection();
            error = "Visita aggiunta con successo.";
            Log4k.debug(this.getClass().getName(), error);
        } catch (notMyPatientException ex) {
            error = "Error in addVisit: ";
            Log4k.warn(this.getClass().getName(),error+ex.getMessage());
        }
        return ret;
    }
   
    /*
     * Aggiunge una visita per presentazione spontanea di un paziente.
     * Da utilizzare solo dopo aver settato patient_id, doctor_id e report.
     */
    public void addContingentVisit(){
        error = "Chiamata addContingentVisit";
        Log4k.debug(this.getClass().getName(), error);
                        
        /* Setta i parametri di default */
        this.program_id = -1;
        this.type = visitKind.PATIENT_REQUEST;
        this.date = this.now();
        this.reminder = 0;
        this.visited = true;        
        
        /* aggiungi la visita al db */
        this.visit_id = this.addVisit();
        
        /* comunica al db e al paziente che la visita è avvenuta */
        this.examine();
        this.report = null;
    }    
    
    /* Invia il referto al db e al paziente.
     * Da utilizzare solo dopo aver settato patient_id, doctor_id, visit_id e report.
     */
    public void examine(){
        /* Invia il referto al db */
        
        dbManager db = new dbManager();
        try {
            error="doctor_id="+doctor_id+" visit_id="+visit_id+" report="+report+" type="+userKind.DOCTOR;
            Log4k.debug(this.getClass().getName(), error);
            db.doExamine(doctor_id, patient_id, visit_id, report, userKind.DOCTOR);
            error="aggiunta visita";
            Log4k.debug(this.getClass().getName(), error);
        } catch (notMyPatientException ex) {
            Log4k.warn(this.getClass().getName(), ex.getMessage());
        }
        this.doctor_name = db.getStrNameByID(this.doctor_id, userKind.DOCTOR);
        db.releaseConnection();
        
        //Invia crea il pdf.
        String path = Macro.localPath("");
        String pdf="";
        String object = "Report for the visit on request"; 
        try {
            String file= pdfPrinter.print(path, this.date.toString(), object, report, this.doctor_name);
            pdf = Macro.url(file);
        } catch (PdfException ex) {
            error = "Errore durante la creazione del pdf: ";
            Log4k.warn(this.getClass().getName(), error+ex.getMessage());
        }
        
        
        /* Invia il referto al paziente */
        String sender = Macro.SYSTEM_ADDRESS;
        dbManager db_report = new dbManager();
        String receivers[] = {db_report.getEmailByID(this.patient_id, userKind.PATIENT)};
        db_report.releaseConnection();
        
        String link = "\n\n"
                + "Il referto può essere trovato al seguente indirizzo: \n"
                + pdf
                + "\n\n"
                + "Attenzione: il link fornito è anonimo e in caso di perdita non potrà essere recuperato!";
                
        
        String subjet = "Visit "+this.visit_id+" of "+this.date;
        Mailer.mail(sender, receivers, subjet, this.report+link);
        
    }
    
    public String urgencyImg(){
        String linkImg = "";
        
        dbManager db = new dbManager();
        int urgent = db.compareForUrgency(date, urgentRange);
        db.releaseConnection();
        
        if (urgent < 0)
            linkImg = linkExpiredImg;
        if (urgent == 0)
            linkImg = linkUrgentImg;
        if (urgent > 0)
            linkImg = linkFutureImg;
        
        error = "Debug urgencyImg. Urgent: "+urgent+" Link: "+linkImg;
        Log4k.debug(this.getClass().getName(), error);
        
        return linkImg;
    }
    
    public String negligentImg(){
        String linkImg = "";
        
        if (this.reminder > 0) linkImg = this.linkNegligentImg;
        else linkImg = this.linkPunctualImg;
        
        return linkImg;
    }
    
    private DateTime now(){
        DateTime ret;
        error = "Chiamata now";
        Log4k.debug(this.getClass().getName(), error);
                
        dbManager db = new dbManager();
        ret = db.now();
        db.releaseConnection();
        
        return ret;
    }
    
    private void setDateByInterval(){
        /*Sommare secondi di this.interval a date per ottenere una nuova data*/
    }
    
    public void flushMessages(){
        this.setVoidReport(false);
    }
    
    
    /**
     * @return the date
     */
    public DateTime getDate() {
        return this.date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(DateTime date) {
        this.date = date;
    }

    /**
     * @return the type
     */
    public visitKind getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(visitKind type) {
        this.type = type;
    }

    /**
     * @return the patient_id
     */
    public int getPatient_id() {
        return patient_id;
    }

    /**
     * @param patient_id the patient_id to set
     */
    public void setPatient_id(int patient_id) {
        this.patient_id = patient_id;
    }
    
    /**
     * @return the patient_name
     */
    public String getPatient_name() {
        return patient_name;
    }

    /**
     * @param patient_name the patient_name to set
     */
    public void setPatient_name(String patient_name) {
        this.patient_name = patient_name;
    }

    /**
     * @return the doctor_id
     */
    public int getDoctor_id() {
        return doctor_id;
    }

    /**
     * @param doctor_id the doctor_id to set
     */
    public void setDoctor_id(int doctor_id) {
        this.doctor_id = doctor_id;
    }

    /**
     * @return the doctor_name
     */
    public String getDoctor_name() {
        return doctor_name;
    }

    /**
     * @param doctor_name the doctor_name to set
     */
    public void setDoctor_name(String doctor_name) {
        this.doctor_name = doctor_name;
    }

    /**
     * @return the program_id
     */
    public int getProgram_id() {
        return program_id;
    }

    /**
     * @param program_id the program_id to set
     */
    public void setProgram_id(int program_id) {
        this.program_id = program_id;
    }    
    
    /**
     * @return the report
     */
    public String getReport() {
        return report;
    }

    /**
     * @param report the report to set
     */
    public void setReport(String report) {
        this.report = report;
    }

    /**
     * @return the reminder
     */
    public int getReminder() {
        return reminder;
    }

    /**
     * @param reminder the reminder to set
     */
    public void setReminder(int reminder) {
        this.reminder = reminder;
    }

    /**
     * @return the visited
     */
    public boolean isVisited() {
        return visited;
    }

    /**
     * @param visited the visited to set
     */
    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    /**
     * @return the visit_id
     */
    public int getVisit_id() {
        return visit_id;
    }

    /**
     * @param visit_id the visit_id to set
     */
    public void setVisit_id(int visit_id) {
        this.visit_id = visit_id;
    }

    /**
     * @return the interval
     */
    public int getInterval() {
        return interval;
    }

    /**
     * @param interval the interval to set
     */
    public void setInterval(int interval) {
        this.interval = interval;
        setDateByInterval();
    }

    /**
     * @return the index
     */
    public int getIndex() {
        return index;
    }

    /**
     * @param index the index to set
     */
    public void setIndex(int index) {
        this.index = index;
    }

    /**
     * @return the voidReport
     */
    public boolean isVoidReport() {
        return voidReport;
    }

    /**
     * @param voidReport the voidReport to set
     */
    public void setVoidReport(boolean voidReport) {
        this.voidReport = voidReport;
    }
    
}
